package xx.iverson.school_manage.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import xx.iverson.school_manage.entity.OperationRecord;
import xx.iverson.school_manage.entity.Points;
import xx.iverson.school_manage.service.OperationRecordService;
import xx.iverson.school_manage.service.PointsService;
import xx.iverson.school_manage.service.TitleService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author wei77
 */
@Controller
public class ToAdminController {

    @Resource
    private PointsService pointsService;


    @Resource
    private TitleService titleService;

    @Resource
    private OperationRecordService operationRecordService;

    // 跳转到登录页面
    @RequestMapping("/")
    public String toLogin(HttpSession session) {
        session.setAttribute("title", titleService.getTitle(1));
        session.setAttribute("smallTitle", titleService.getTitle(2));
        return "login";
    }

    // 跳转到管理员首页
    @RequestMapping("toAdminIndex")
    public String toAdminIndex(@RequestParam(value = "letter", defaultValue = "") String letter, @RequestParam(value = "name", defaultValue = "") String name, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               Model model) {
        if (name != null && !name.isEmpty()) {
            PageHelper.startPage(pageNum, 100);
            List<Points> points = pointsService.getLikePoints(name);
            PageInfo<Points> pageInfo = new PageInfo<>(points);
            model.addAttribute("points", pageInfo);
            model.addAttribute("pageNum", pageInfo.getPageNum());
            model.addAttribute("pages", pageInfo.getPages());
        } else if (letter != null && !letter.isEmpty()) {
            PageHelper.startPage(pageNum, 100);
            List<Points> points = pointsService.getLetterPoints(letter);
            PageInfo<Points> pageInfo = new PageInfo<>(points);
            model.addAttribute("points", pageInfo);
            model.addAttribute("pageNum", pageInfo.getPageNum());
            model.addAttribute("pages", pageInfo.getPages());
        } else {
            // 开始分页
            PageHelper.startPage(pageNum, 10);
            List<Points> points = pointsService.getListPoints(0);
            PageInfo<Points> pageInfo = new PageInfo<>(points);

            model.addAttribute("points", pageInfo);
            model.addAttribute("pageNum", pageInfo.getPageNum());
            model.addAttribute("pages", pageInfo.getPages());

        }

        return "admin/adminIndex";
    }

    // 跳转到饼图页面
    @RequestMapping("toBar")
    public String toBar() {
        return "admin/bar";
    }

    // 获取饼图数据
    @RequestMapping("getBarData")
    @ResponseBody
    public Map<String, Integer> getBarData() {
        return pointsService.getPointsDistribution();
    }

    // 去设置页面
    @RequestMapping("toSetting")
    public String toSetting() {
        return "admin/set";
    }
    //查看记录
    @RequestMapping("getAdminOperationRecords")
    @ResponseBody
    public List<OperationRecord> getAdminOperationRecords(Integer userId) {
        return operationRecordService.getOperationRecords(userId);
    }
}
